package com.ichifinga.airwave.shared.entities;
import android.content.*;

public class PhraseLibrary
{
	private static PhraseLibrary _instance;
	private final int _phraseLimit;
	private final boolean _isLite;
	private Phrase _currentDisplayPhrase;
	private Phrase _currentEditPhrase;
	
	private static Phrase GetPhrase(Context context, int phraseId)
	{		
		SharedPreferences sharedPreferences;
    	sharedPreferences = context.getSharedPreferences("SharedPreferences", Context.MODE_WORLD_WRITEABLE);

    	Phrase phrase = new Phrase();

		phrase._phraseId = phraseId;
		phrase._textToDisplay = sharedPreferences.getString("TextToDisplay" + phraseId, "Phrase" + phraseId).toUpperCase();
		phrase._textSize = TextSizes.values()[sharedPreferences.getInt("TextSize" + phraseId, TextSizes.Small.ordinal())];
		phrase._textWidth = TextWidths.values()[sharedPreferences.getInt("TextWidth" + phraseId, TextWidths.Thin.ordinal())];
		phrase._textShade = TextShades.values()[sharedPreferences.getInt("TextShade" + phraseId, TextShades.Fire.ordinal())];
		phrase._colourOne = Colours.values()[sharedPreferences.getInt("ColourOne" + phraseId, Colours.White.ordinal())];
		phrase._colourTwo = Colours.values()[sharedPreferences.getInt("ColourTwo" + phraseId, Colours.Red.ordinal())];
		phrase._colourThree = Colours.values()[sharedPreferences.getInt("ColourThree" + phraseId, Colours.LightRed.ordinal())];
		phrase._backgroundColour = Colours.values()[sharedPreferences.getInt("BackgroundColour" + phraseId, Colours.Black.ordinal())];
		phrase._shadeType = ShadeTypes.values()[sharedPreferences.getInt("ShadeType" + phraseId, ShadeTypes.Gradient.ordinal())];
		phrase._displaySpeed =  DisplaySpeeds.values()[sharedPreferences.getInt("DisplaySpeed" + phraseId, DisplaySpeeds.Normal.ordinal())];
		phrase._scrollType = ScrollTypes.values()[sharedPreferences.getInt("ScrollType" + phraseId, ScrollTypes.Sequence.ordinal())];
		phrase._flashType = FlashTypes.values()[sharedPreferences.getInt("FlashType" + phraseId, FlashTypes.Pulse.ordinal())];

		return phrase;
	}
	
	private PhraseLibrary()
	{
		_isLite = retreiveIsLiteFromVersion();
		_phraseLimit = retreivePhraseLimitFromVersion();
	}

	private boolean retreiveIsLiteFromVersion()
	{
		return ApplicationContext.getInstance().getPackageName().toLowerCase().contains("lite");
	}

	private int retreivePhraseLimitFromVersion()
	{
		return _isLite ? 2 : 20;
	}

	public int getPhraseLimit()
	{
		return _phraseLimit;
	}
	
	public boolean getIsLite()
	{
		return _isLite;
	}
	
	public static void createInstance(Context context)
	{
		if(_instance == null)
		{
			synchronized (PhraseLibrary.class)
			{
				if(_instance == null)
				{
					_instance = new PhraseLibrary();
				}
			}
		}
	}
	
	public static PhraseLibrary getInstance()
	{
		return _instance;
	}
}
